package com.kefu.chat.controller;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.chat.model.ChatFaq;
import com.kefu.chat.model.ChatFaqGroup;
import com.kefu.chat.model.ChatFaqGroupDocRel;
import com.kefu.chat.service.ChatFaqGroupDocRelService;
import com.kefu.chat.service.ChatFaqGroupService;
import com.kefu.chat.service.ChatFaqService;
import com.kefu.chat.vo.*;
import com.kefu.common.context.KefuContext;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.util.Assert;
import com.kefu.common.vo.BatchVo;
import com.kefu.common.vo.DeleteVo;
import com.kefu.common.vo.DetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@Api(tags="常见问题")
@RestController
@RequestMapping("/chat/faq/")
public class ChatFaqController extends AbstractBaseController{
    @Resource
    private ChatFaqService chatFaqService;
    @Resource
    private ChatFaqGroupService chatFaqGroupService;
    @Resource
    private ChatFaqGroupDocRelService chatFaqGroupDocRelService;

    @PostMapping("add")
    @ApiOperation(value="添加常见问题方案", notes="添加常见问题方案")
    public Result<String> add(@Validated @RequestBody ChatFaqInsertVo chatFaqVo) {
        logger.info("添加方案:{}",chatFaqVo);

        long count = chatFaqService.count(Wrappers.lambdaQuery(ChatFaq.class)
                .eq(ChatFaq::getTenantId, KefuContext.getTid())
                .eq(ChatFaq::getFaqName, chatFaqVo.getFaqName()));
        Assert.isTrue(count > 0, "常见问题方案"+ chatFaqVo.getFaqName()+"已经存在！");
        ChatFaq info = chatFaqVo.convert();
        chatFaqService.save(info);
        return ResultGenerator.genSuccessResult(info.getId());
    }

    @PostMapping("update")
    @ApiOperation(value="修改常见问题方案", notes="添加常见问题方案")
    public Result<String> update(@Validated @RequestBody ChatFaqUpdateVo chatFaqVo) {
        logger.info("修改常见问题方案:{}",chatFaqVo);

        List<ChatFaq> list = chatFaqService.list(Wrappers.lambdaQuery(ChatFaq.class).eq(ChatFaq::getTenantId, KefuContext.getTid())
                .eq(ChatFaq::getFaqName, chatFaqVo.getFaqName()));
        if (CollUtil.isNotEmpty(list)) {
            boolean flag = true;
            for (ChatFaq chatFaq :
                    list) {
                if (!chatFaq.getId().equals(chatFaqVo.getId())) {
                    flag = false;
                    break;
                }
            }
            Assert.isTrue(!flag, "方案名" + chatFaqVo.getFaqName() + "已经存在！");
        }
        ChatFaq info = chatFaqVo.convert();
        chatFaqService.update(info);
        return ResultGenerator.genSuccessResult(info.getId());
    }

    @PostMapping("detail")
    @ApiOperation(value="常见问题方案详情", notes="常见问题方案详情")
    public Result<ChatFaq> detail(@Validated @RequestBody DetailVo detail) {
        logger.info("常见问题方案详情:{}",detail);

        ChatFaq info = chatFaqService.getById(detail.getId());
        return ResultGenerator.genSuccessResult(info);
    }

    @PostMapping("batchDelete")
    @ApiOperation(value="批量删除常见问题方案", notes="批量删除常见问题方案")
    public Result<String> batchDelete(@Validated @RequestBody BatchVo batch) {
        logger.info("批量删除常见问题方案:{}",batch);

        chatFaqService.batchDelete(batch.getIds());
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("delete")
    @ApiOperation(value="删除常见问题方案", notes="删除常见问题方案")
    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
        logger.info("删除常见问题方案:{}",delete);

        chatFaqService.deleteById(delete.getId());
        return ResultGenerator.genSuccessResult(delete.getId());
    }

    @PostMapping("list")
    @ApiOperation(value="查询常见问题方案", notes="查询常见问题方案")
    public Result<Page<ChatFaq>> list(@Validated @RequestBody ChatFaqSearchVo search) {

        Page<ChatFaq> page = chatFaqService.listFaq(search);
        return ResultGenerator.genSuccessResult(page);
    }

    @PostMapping("addGroup")
    @ApiOperation(value="添加常见问题库分组", notes="添加常见问题库分组")
    public Result<String> addGroup(@Validated @RequestBody ChatFaqGroupInsertVo chatFaqGroupVo) {
        logger.info("添加常见问题库分组:{}",chatFaqGroupVo);

        ChatFaqGroup info = chatFaqGroupVo.convert();
        chatFaqGroupService.save(info);
        return ResultGenerator.genSuccessResult(info.getId());
    }

    @PostMapping("updateGroup")
    @ApiOperation(value="修改常见问题库分组", notes="修改常见问题库分组")
    public Result<String> updateGroup(@Validated @RequestBody ChatFaqGroupUpdateVo chatFaqGroupVo) {
        logger.info("修改常见问题库分组:{}",chatFaqGroupVo);

        ChatFaqGroup info = chatFaqGroupVo.convert();
        chatFaqGroupService.update(info);
        return ResultGenerator.genSuccessResult(info.getId());
    }

    @PostMapping("deleteGroup")
    @ApiOperation(value="删除常见问题库分组", notes="删除常见问题库分组")
    public Result<String> deleteGroup(@Validated @RequestBody DeleteVo delete) {
        logger.info("删除常见问题库分组:{}",delete);

        List<ChatFaqGroupDocRel> list = chatFaqGroupDocRelService.list(Wrappers.lambdaQuery(ChatFaqGroupDocRel.class)
                .eq(ChatFaqGroupDocRel::getGroupId, delete.getId()));
        Assert.isNotEmpty(list, "存在关联问题，不能删除!");
        chatFaqGroupService.deleteById(delete.getId());
        return ResultGenerator.genSuccessResult(delete.getId());
    }

    @PostMapping("treeGroup")
    @ApiOperation(value="查询常见问题库分组", notes="查询常见问题库分组")
    public Result<List<?>> treeGroup(@Validated @RequestBody ChatFaqGroupSearchVo search) {
        logger.info("查询常见问题库分组:{}",search);

        return ResultGenerator.genSuccessResult(chatFaqGroupService.treeGroup(search));
    }

    @PostMapping("addFaqGroupDoc")
    @ApiOperation(value="添加常见问题词条", notes="添加常见问题")
    public Result<String> add(@Validated @RequestBody ChatFaqGroupDocInsertVo chatFaqGroupDOcVo) {
        logger.info("添加常见问题词条:{}",chatFaqGroupDOcVo);

        return ResultGenerator.genSuccessResult(chatFaqGroupDocRelService.addDoc(chatFaqGroupDOcVo));
    }

    @PostMapping("delFaqGroupDoc")
    @ApiOperation(value="删除常见问题词条", notes="删除常见问题")
    public Result<String> delFaqGroupDoc(@Validated @RequestBody DeleteVo delete) {
        logger.info("删除常见问题词条:{}",delete);
        List<ChatFaqGroupDocRel> list = chatFaqGroupDocRelService.list(Wrappers.lambdaQuery(ChatFaqGroupDocRel.class)
                .eq(ChatFaqGroupDocRel::getDocId, delete.getId()));
        Assert.isNotEmpty(list, "常见问题已经被关联不能删除");
        chatFaqGroupDocRelService.deleteById(delete.getId());
        return ResultGenerator.genSuccessResult(delete.getId());
    }

    @PostMapping("getGroupDocList")
    @ApiOperation(value="查询分组下问题词条", notes="查询分组下问题")
    public Result<List<ChatFaqGroupDocRel>> getGroupDocList(@Validated @RequestBody ChatFaqGroupDocRelSearchVo search) {
        logger.info("查询分组下问题:{}",search);

        return ResultGenerator.genSuccessResult(chatFaqGroupDocRelService.getGroupDocList(search));
    }
}
